package org.mclll.xwangplugin.questTracking.questGoal;

import me.pikamug.quests.events.quester.BukkitQuesterPostUpdateObjectiveEvent;
import me.pikamug.quests.quests.components.BukkitObjective;
import me.pikamug.quests.quests.components.Stage;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.entity.Item;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scoreboard.Objective;
import org.jetbrains.annotations.NotNull;
import org.mclll.xwangplugin.questTracking.persistence.Persistence;
import org.mclll.xwangplugin.util.GetChineseNameUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
 * @author: geduo
 * @date: 2023/10/13 16:26
 */
public class DeliveryToNpcQuestTrack {

    private static final String LINE_PREFIX_FORMAT = "交给%s：%s";
    private static final String LINE_FORMAT = LINE_PREFIX_FORMAT + " x %d";

    public static void questTracking(Objective context, Integer line, Stage questsStage) {
        LinkedList<UUID> itemDeliveryTargets = questsStage.getItemDeliveryTargets();
        LinkedList<ItemStack> itemsToDeliver = (LinkedList<ItemStack>) questsStage.getItemsToDeliver();
        if (itemDeliveryTargets.isEmpty() || CitizensAPI.getNPCRegistry() == null) {
            return;
        }
        for (int i = 0; i < itemDeliveryTargets.size(); i++) {
            UUID npcUuid = itemDeliveryTargets.get(i);
            NPCRegistry npcRegistry = CitizensAPI.getNPCRegistry();
            NPC npc = npcRegistry.getByUniqueIdGlobal(npcUuid);
            ItemStack itemToNpc = itemsToDeliver.get(i);
            context.getScore(getGoalFormat(npc.getName(), GetChineseNameUtil.get(itemToNpc.getType()), itemToNpc.getAmount()))
                    .setScore(line--);
        }
    }

    @NotNull
    public static String getGoalFormat(String npcName, String itemName, Integer count) {
        return ChatColor.YELLOW + String.format(LINE_FORMAT, npcName, itemName, count);
    }

    @NotNull
    public static String getFinishFormat(String npcName, String itemName) {
        return ChatColor.GRAY + String.format(LINE_PREFIX_FORMAT, npcName, itemName) + " (✓)";
    }

    public static void goalFinish(BukkitQuesterPostUpdateObjectiveEvent event, List<Persistence.SerializeScore> scoreboard) {
        NPCRegistry npcRegistry = CitizensAPI.getNPCRegistry();
        if (npcRegistry == null) {
            return;
        }
        BukkitObjective questsObj = (BukkitObjective) event.getObjective();
        List objList = (List) questsObj.getGoalObject();
        UUID npcUuid = (UUID) objList.get(0);
        ItemStack itemStack = (ItemStack) objList.get(1);
        String itemName = GetChineseNameUtil.get(itemStack.getType());
        NPC npc = npcRegistry.getByUniqueIdGlobal(npcUuid);
        for (Persistence.SerializeScore score : scoreboard) {
            String entry = score.getName();
            if (entry.startsWith(ChatColor.YELLOW + "交给" + npc.getName()) && entry.contains(itemName)) {
                if (questsObj.getGoal() - questsObj.getProgress() == 0) {
                    score.setName(getFinishFormat(npc.getName(), itemName));
                    score.setValue(scoreboard.get(scoreboard.size() - 1).getValue() - 1);
                } else {
                    score.setName(getGoalFormat(npc.getName(), itemName, questsObj.getGoal() - questsObj.getProgress()));
                }
                break;
            }
        }

    }


}
